_VERTICAL = 1001
_HORIZONTAL = 1002
_NOCHANGE = 1003
_VBARREDEF = 2.0

barreDef ={}
barreDef.count = 0
meta_ = {__index = barreDef}

-- New_barreDef(valeur mini, valeur maxi, carre de la fenetre d'affichage
function barreDef.new(vmax,x1,y1,x2,y2)
	local Bd = {"dir","pcur","vmax","x1","y1","x2","y2","coulCadre","coulFond","coulCurs","coulFleche"}
	if vmax == nil then vmax = 10 end
	if x1 == nil then x1 = 246 end
	if y1 == nil then y1 = 0 end
	if x2 == nil then x2 = 256 end
	if y2 == nil then y2 = 192 end
	Bd.pcur = 1	-- position curseur
	Bd.vmax = vmax	-- valeur maxi
	Bd.y1 = y1	-- 
	Bd.y2 = y2	-- { coin superieur gauche et inferieur droite de la 
	Bd.x1 = x1	-- { fenetre d'affichage de la barre de defilement
	Bd.x2 = x2	--
	if x2-x1 < y2-y1 then
		Bd.dir = _VERTICAL
		Bd.x2 = Bd.x1+10
	else
		Bd.dir = _HORIZONTAL
		Bd.y2 = Bd.y1+10
	end
	Bd.coulCadre = Color.new(31,31,31) --couleur du cadre
	Bd.coulCurs = Color.new(31,31,12)  --couleur du curseur
	Bd.coulFond = Color.new(0,0,0)	  --couleur du fond
	Bd.coulFleche = Bd.coulCadre		  --couleur des fleches
	barreDef.count = barreDef.count+1
	
	return setmetatable(Bd,meta_)
end

function barreDef:del()
	self = nil
end

function barreDef:show(ecran)
	local ep_curs, po_curs
	if ecran ~= SCREEN_UP then ecran = SCREEN_DOWN end

	-- fond
	screen.drawFillRect(ecran,self.x1,self.y1,self.x2,self.y2,self.coulFond)
	if self.dir == _VERTICAL then
		ep_curs = ((self.y2-10)-(self.y1+10))/self.vmax
		po_curs = math.floor(ep_curs*(self.pcur-1))+10+self.y1
		ep_curs = math.floor(ep_curs)
		if ep_curs == 0 then ep_curs = 1 end
		-- Fleche haut
		screen.drawLine(ecran,self.x1+4,self.y1+2,self.x1+4,self.y1+8,self.coulFleche)
		screen.drawLine(ecran,self.x1+5,self.y1+2,self.x1+5,self.y1+8,self.coulFleche)
		screen.drawLine(ecran,self.x1+3,self.y1+3,self.x1+7,self.y1+3,self.coulFleche)
		screen.drawLine(ecran,self.x1+2,self.y1+4,self.x1+8,self.y1+4,self.coulFleche)
		-- Fleche bas
		screen.drawLine(ecran,self.x1+4,self.y2-2,self.x1+4,self.y2-8,self.coulFleche)
		screen.drawLine(ecran,self.x1+5,self.y2-2,self.x1+5,self.y2-8,self.coulFleche)
		screen.drawLine(ecran,self.x1+3,self.y2-4,self.x1+7,self.y2-4,self.coulFleche)
		screen.drawLine(ecran,self.x1+2,self.y2-5,self.x1+8,self.y2-5,self.coulFleche)
		-- Curseur
		screen.drawFillRect(ecran,self.x1+1,po_curs,self.x1+9,po_curs+ep_curs,self.coulCurs)
		-- Cadre fleche
		screen.drawLine(ecran,self.x1,self.y1+9,self.x1+10,self.y1+9,self.coulCadre)
		screen.drawLine(ecran,self.x1,self.y2-10,self.x2,self.y2-10,self.coulCadre)
	else
		ep_curs = ((self.x2-10)-(self.x1+10))/self.vmax
		po_curs = math.floor(ep_curs*(self.pcur-1))+10+self.x1
		ep_curs = math.floor(ep_curs)
		if ep_curs == 0 then ep_curs = 1 end
		-- Fleche gauche
		screen.drawLine(ecran,self.x1+2,self.y1+4,self.x1+8,self.y1+4,self.coulFleche)
		screen.drawLine(ecran,self.x1+2,self.y1+5,self.x1+8,self.y1+5,self.coulFleche)
		screen.drawLine(ecran,self.x1+3,self.y1+3,self.x1+3,self.y1+7,self.coulFleche)
		screen.drawLine(ecran,self.x1+4,self.y1+2,self.x1+4,self.y1+8,self.coulFleche) 
		-- Fleche droite
		screen.drawLine(ecran,self.x2-2,self.y1+4,self.x2-8,self.y1+4,self.coulFleche)
		screen.drawLine(ecran,self.x2-2,self.y1+5,self.x2-8,self.y1+5,self.coulFleche)
		screen.drawLine(ecran,self.x2-4,self.y1+3,self.x2-4,self.y1+7,self.coulFleche)
		screen.drawLine(ecran,self.x2-5,self.y1+2,self.x2-5,self.y1+8,self.coulFleche)
		-- Curseur
		screen.drawFillRect(ecran,po_curs,self.y1+1,po_curs+ep_curs,self.y1+9,self.coulCurs)
		-- Cadre fleche
		screen.drawLine(ecran,self.x1+9,self.y1,self.x1+9,self.y1+10,self.coulCadre)
		screen.drawLine(ecran,self.x2-10,self.y1,self.x2-10,self.y2,self.coulCadre)
	end
	-- Cadre
	screen.drawRect(ecran,self.x1,self.y1,self.x2,self.y2,self.coulCadre)
end

function barreDef:held()
	if Stylus.newPress then
		if Stylus.X > self.x1 and Stylus.X < self.x2 then
			if Stylus.Y > self.y1 and Stylus.Y < self.y2 then
				if self.dir == _VERTICAL then
					if Stylus.Y > self.y1 and Stylus.Y < self.y1+10 then
						if self.pcur > 1 then self.pcur = self.pcur -1 end
					end
					if Stylus.Y > self.y2-10 and Stylus.Y < self.y2 then
						if self.pcur < self.vmax then self.pcur = self.pcur +1 end
					end
				else
					if Stylus.X > self.x1 and Stylus.X < self.x1+10 then
						if self.pcur > 1 then self.pcur = self.pcur -1 end
					end
					if Stylus.X > self.x2-10 and Stylus.X < self.x2 then
						if self.pcur < self.vmax then self.pcur = self.pcur +1 end
					end
				end
			end
		end
	end
end

function barreDef:move(x,y)
	local larg = self.x2-self.x1
	local haut = self.y2-self.y1
	self.x1 = x
	self.y1 = y
	self.x2 = x+larg
	self.y2 = y+haut
end

function barreDef:changeCoul(coulCadre,coulCurs,coulFond,coulFleche)
	if coulCadre == nil then coulCadre = _NOCHANGE end
	if coulCurs == nil then coulCurs = _NOCHANGE end
	if coulFond == nil then coulFond = _NOCHANGE end
	if coulFleche == nil then coulFleche = _NOCHANGE end
	if coulCadre ~= _NOCHANGE then self.coulCadre = coulCadre end
	if coulCurs ~= _NOCHANGE then self.coulCurs = coulCurs end
	if coulFond ~= _NOCHANGE then self.coulFond = coulFond end
	if coulFleche ~= _NOCHANGE then self.coulFleche = coulFleche end
end

function barreDef:posCurs(poscurs)
	if poscurs ~= nil then
		if poscurs > 0 and poscurs < self.vmax+1 then
			self.pcur = poscurs
		end
	end
	return self.pcur
end

function barreDef:vMax(vmax)
	if vmax ~= nil then
		if vmax > 0 then
			self.vmax = vmax
		end
	end
	return self.vmax
end
